package com.hundsun.cprs.yyt.redis;


import java.util.List;
import java.util.concurrent.TimeUnit;

import redis.clients.jedis.Jedis;

public class Consumer extends Thread{
    public static final String MESSAGE_KEY = "message:queue";
    private Jedis jedis;
    private String consumerName;
    private volatile int count;
    
    public Consumer(String name){
        this.consumerName = name;
        init();
    }
    
    private void init(){
        jedis = new Jedis("192.168.78.101", 6379); 
        jedis.auth("redis@123");
    }
    
    public void processMessage(){
        List<String> messsage = jedis.brpop(0, MESSAGE_KEY);
        System.out.println("消息消息成功,当前消息：" + messsage.get(0));
        count++;
    }

    @Override
    public void run() {
        
        while(true){
            try {
               processMessage();
                TimeUnit.SECONDS.sleep(2);
               System.out.println(this.consumerName + "处理完成" + System.currentTimeMillis());
            } catch (InterruptedException e) {
                 e.printStackTrace();
            }
        }
    }
    
    public static void main(String[] args) {
        for(int i = 0;i<2;i++){
            Consumer producer = new Consumer("test-message" + i);
            producer.start();
        }
        
        try {
            Thread.sleep(200000);
        } catch (InterruptedException e) {
             e.printStackTrace();
        }
    }
}
